<?php
class network{
	
	private $_webroot;
	
	
	function __construct()
	{
	   include "../../config/my.php";
	   $this->_webroot=$config->fileRoot.'config/';  
	}
	/**
	 * 得到系统的配置信息
	 */
    public function getNetworkConfig() {
        try {
        	// echo "<pre>";
        	$result=array();
        	$file_pos=$this->_webroot.'network.ini';
            $file_content=trim(file_get_contents($file_pos));
            $file_arr=explode("\n",$file_content);
            foreach($file_arr as $key=>$val){
            	$arr=explode("=",$val);
            	$result[$arr[0]]=$arr[1];
            }
            return $result;
           // var_dump($result);
        }
        catch (Exception $e){
            $result = array();
            var_dump($e->getMessage() . $e->getTraceAsString());
        }
        return 0;
    }
    
    /**
     * 设置系统的配置信息
     */
    public function setNetworkConfig($str){
    	try {
        	$file_pos=$this->_webroot.'network.ini';
            $flag=file_put_contents($file_pos,$str);
            return $flag;
        }
        catch (Exception $e){
            $result = array();
            var_dump($e->getMessage() . $e->getTraceAsString());
        }
        return 0;
    	
    }
    
	/**
     * 得到系统的网卡信息
     */
    public function getNetworkSetting(){
    	$result=array();
    	
    	$network_config=$this->getNetworkConfig();
    	$network_name_arr=explode(',',$network_config['name']);
    	//var_dump($network_name_arr);
    	foreach($network_name_arr as $key=>$val){
    		$result[$val]['name']=$val;
    		$result[$val]=$this->getNetworkByName($val);
    	}
    	return $result;
    }
    
    
    public function getNetworkByName($ethname){
    	//echo "<pre>";
    	$result=array();
    	$result['wan_enable']=0;
		$result['wan_ipv4_enable']=0;
		$result['wan_ipv4_type']='static';
		$result['wan_ipv4_add']='';
		$result['wan_ipv4_mask']='';
		$result['wan_ipv4_gw']='';
		$result['wan_ipv4_dns1']='';
		$result['wan_ipv4_dns2']='';
		$result['wan_ipv6_enable']=0;
		$result['wan_ipv6_add'] = '';
		$result['wan_ipv6_mask'] = '';
		$result['wan_ipv6_gw'] = '';
		
    	$network_config=$this->getNetworkConfig();
    	$network_file=file_get_contents($network_config['postion']);
    	preg_match('/#'.$ethname.' begin(.*?)#'.$ethname.' end/is',$network_file,$array1);
    	//var_dump($array1[1]);
		$wan1inf=trim($array1[1])."\r\n";
		$result['wan_enable'] = ($array1[1]!=NULL) ? 1:0;//判断是否存在
		if($result['wan_enable']){
		    preg_match('/iface '.$ethname.' inet (.*?)\s/i',$wan1inf,$array2);
		    $result['wan_ipv4_enable'] = empty($array2[1])?0:1;
		    if($result['wan_ipv4_enable']){
		        $result['wan_ipv4_type']=$array2[1];
		        $ipv4_match = '(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])';
		        preg_match('/address '.$ipv4_match.'\s/',$wan1inf,$array4);
		        $result['wan_ipv4_add']=substr(trim($array4[0]),8);
		        preg_match('/netmask '.$ipv4_match.'\s/',$wan1inf,$array5);
		        $result['wan_ipv4_mask']=substr(trim($array5[0]),8);
		        preg_match('/gateway '.$ipv4_match.'\s/',$wan1inf,$array6);
		    
		        $result['wan_ipv4_gw']=substr(trim(@$array6[0]),8);
		        preg_match('/dns-nameservers '.$ipv4_match.'(.*)\s/',$wan1inf,$array10);
		   		
		       // echo '<pre>';
		       // var_dump($wan1inf);
		       // var_dump($array10);
		        $dns = substr(trim(@$array10[0]),16);
		   
		        $dns_array = explode(' ',$dns);
		       
		        $result['wan_ipv4_dns1']=$dns_array[0];
		        if(sizeof($dns_array)==2) $result['wan_ipv4_dns2']=$dns_array[1];
		    }
		    preg_match('/iface '.$ethname.' inet6 (.*?)\s/i',$wan1inf,$array3);
		    $result['wan_ipv6_enable'] = empty($array3[1])?0:1;
		    if($result['wan_ipv6_enable']){
		        $ipv6_match = '(.*?)\:(.*?)\s';
		        preg_match('/address '.$ipv6_match.'\s/',$wan1inf,$array7);
		        $result['wan_ipv6_add']=substr(trim($array7[0]),8);
		        preg_match('/netmask \d+\s/',$wan1inf,$array8);
		        $result['wan_ipv6_mask']=substr(trim($array8[0]),8);
		        preg_match('/gateway '.$ipv6_match.'\s/',$wan1inf,$array9);
		        $result['wan_ipv6_gw']=substr(trim(@$array9[0]),8);
		    }
		}
		else $result['wan_ipv4_type']='disabled';
		return $result;
    }
    
    
    /**
     * 设置系统的网卡信息
     */
    public function setNetworkSetting($str){
       $source_arr=array();
       /*$str_arr=explode("&",$str);
       foreach($str_arr as $key =>$val){
       		$arr=explode('=',$val);
       		$source_arr[$arr[0]]=$arr[1];
       }*/
       $source_arr=$str;
       $network_eth=$this->getNetworkConfig();
       $network_eth_arr=explode(',',trim($network_eth['name']));
       $str = "";
	   $str .="#config by saomiao\r\n\r\n#lo begin\r\nauto lo\r\niface lo inet loopback\r\n#lo end\r\n\r\n";
       foreach($network_eth_arr as $key =>$val){
       	  $str.=$this->setNetworkByName($source_arr,$val);
       }
       $handle=fopen(trim($network_eth['postion']),'w');
	   fwrite($handle,$str);
	   fclose($handle);
		
	   $inf = shell_exec(trim($network_eth['cmd']));
	   if(strpos($inf,'done')) return true;
	   if(strpos($inf,'Failed')) return false;
	   //var_dump($network_eth);
    }
	
    
    /**
     *根据名称得到要更改设置的字符串
     */
     public function setNetworkByName($source_arr,$ethname){
       $str = "";
       $wan_proto=$source_arr["wan_proto_".$ethname];
       if($wan_proto=='static'){
		    $wan_enable=1;
		    $wan_ipv4_enable=isset($source_arr["ipv4enable_".$ethname])?1:0;
		    if($wan_ipv4_enable){
		        $wan_ipv4_type='static';
		        $wan_ipv4_add=$source_arr["ipv4_ipaddr_".$ethname];
		        $wan_ipv4_mask=$source_arr["ipv4_netmask_".$ethname];
		        $wan_ipv4_gw=$source_arr["ipv4_gateway_".$ethname];
		    }
		    $wan_ipv6_enable=isset($source_arr["ipv6enable_".$ethname])?1:0;
		    if($wan_ipv6_enable){
		        $wan_ipv6_type='static';
		        $wan_ipv6_add=$source_arr["ipv6_ipaddr6_".$ethname];
		        $wan_ipv6_mask=$source_arr["ipv6_netmask6_".$ethname];
		        $wan_ipv6_gw=$source_arr["ipv6_gateway6_".$ethname];
		    }    
		}
		elseif($wan_proto=='dhcp'){
		    $wan_enable=1;
		    $wan_ipv4_enable=1;
		    $wan_ipv4_type='dhcp';
		}
		elseif($wan_proto=='disabled'){
		    $wan_enable=0;
		}
		$wan_ipv4_dns=intval($source_arr["dns_auto_".$ethname]);
		$wan_ipv4_dns1=($source_arr["dns_1_".$ethname]!='')?$source_arr["dns_1_".$ethname]:0;
		$wan_ipv4_dns2=($source_arr["dns_2_".$ethname]!='')?$source_arr["dns_2_".$ethname]:0;
		if($wan_enable) {
		    $str .= "#$ethname begin\r\nauto $ethname\r\n";
		    if($wan_ipv4_enable) $str .= "iface $ethname inet $wan_ipv4_type\r\n";
		    if($wan_ipv4_type == 'static') {
		        $str .= "address $wan_ipv4_add\r\nnetmask $wan_ipv4_mask\r\n";
		        if($wan_ipv4_gw) $str .= "gateway $wan_ipv4_gw\r\n";
		        if($wan_ipv4_dns){
		        	if($wan_ipv4_dns1){
		            	$str .= "dns-nameservers $wan_ipv4_dns1";
		        	}
		            if($wan_ipv4_dns2) $str .= " $wan_ipv4_dns2";
		            $str .= "\r\n";
		        }
		    }
		    if($wan_ipv6_enable) $str .= "iface $ethname inet6 static\r\naddress $wan_ipv6_add\r\nnetmask $wan_ipv6_mask\r\n";
		    if($wan_ipv6_gw) $str .= "gateway $wan_ipv6_gw\r\n";
		    $str .="#$ethname end\r\n\r\n";
		}
		
		return $str;
     }
}
?>
